package com.tanhua.app.Service;

import cn.hutool.core.collection.CollUtil;
import com.tanhua.api.FriendApi;
import com.tanhua.api.UserApi;
import com.tanhua.api.UserInfoApi;
import com.tanhua.app.exception.BusinessException;
import com.tanhua.app.interceptor.UserThreadLocal;
import com.tanhua.autoconfig.template.HuanXinTemplate;
import com.tanhua.commons.utils.Constants;
import com.tanhua.model.mongo.Friend;
import com.tanhua.model.pojo.User;
import com.tanhua.model.pojo.UserInfo;
import com.tanhua.model.vo.ContactVo;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

@Service
public class MessagesService {
    @DubboReference
    private UserApi userApi;
    @DubboReference
    private UserInfoApi userInfoApi;
    @Autowired
    private HuanXinTemplate huanXinTemplate;
    @DubboReference
    private FriendApi friendApi;
    //根据环信id查询用户详细信息
    public UserInfoVo findUserinfo(String huanxinId) {
      User user= userApi.findByhuanxinId(huanxinId);
        UserInfo userInfo = userInfoApi.findByid(user.getId());
        UserInfoVo userInfoVo = new UserInfoVo();
        BeanUtils.copyProperties(userInfo,userInfoVo);
        Integer age = userInfo.getAge();
        userInfoVo.setAge(age.toString());
        return userInfoVo;
    }

    /**
     * 添加好友
     * @param userId
     */
    public void contacts(Long userId) {
        //1,添加好友到环信
        Boolean aBoolean = huanXinTemplate.addContact(Constants.HX_USER_PREFIX + UserThreadLocal.getid(), Constants.HX_USER_PREFIX + userId);
        if (!aBoolean) {
            throw new BusinessException(ErrorResult.error());
        }
        friendApi.save(userId, UserThreadLocal.getid());


    }

    /**
     * 删除好友
     * @param userId
     */
    public  void deleteContacts(Long userId){
        //1,删除好友到环信
        Boolean aBoolean = huanXinTemplate.deleteContact(Constants.HX_USER_PREFIX + UserThreadLocal.getid(), Constants.HX_USER_PREFIX + userId);
        if (!aBoolean) {
            throw new BusinessException(ErrorResult.error());
        }
        friendApi.delete(userId, UserThreadLocal.getid());

    }



    /**
     * 查询联系人列表
     * @param page
     * @param pagesize
     * @param keyword
     * @return
     */
    public PageResult contactsList(Integer page, Integer pagesize, String keyword) {
      List<Friend> friendList= friendApi.findList(UserThreadLocal.getid());
      if (CollUtil.isEmpty(friendList)){
          return new PageResult();
      }
        List<Long> friends = friendList.stream().map(Friend::getFriendId).collect(Collectors.toList());
    PageResult<UserInfo> userInfoList= userInfoApi.selectPage(friends,page,pagesize,keyword);
        List<UserInfo> items = userInfoList.getItems();
        ArrayList<ContactVo> list = new ArrayList<>();
        for (UserInfo item : items) {
            ContactVo vo = ContactVo.init(item);
            list.add(vo);
        }
return new PageResult(page,pagesize,0,list);
    }
}
